//
//  MiaoShaModel.swift
//
//  Created by eme on 2017/2/20
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol
import SwiftDate



class MiaoShaModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kMiaoShaDescriptionValueKey: String = "description"
    internal let kMiaoShaActiveKey: String = "active"
    internal let kMiaoShaTimeKey: String = "time"
    internal let kMiaoShaLefttimeKey: String = "lefttime"
    // MARK: 属性
    var descriptionValue: String
    var active: Int
    var time: String
    var lefttime: Double
    
    var header_desc:String{
        get{
            switch self.item_state {
            case 1:
                return "限时限量抢购中"
            case 2:
                return "限时限量抢购中"
            default:
                return "抢购即将开启"
            }
        }
    }
    /// 当前状态类型
    var item_state:Int{
        get{
            if self.lefttime > 0 {
                return 3
            }else if self.active == 1{
                return 2
            }else{
                return 1
            }
        }
    }
    ///将time转换成时间格式
    var serverTime:Date?{
        get{
            do{
                return try self.time.date(format: DateFormat.custom("yyyy-MM-dd HH:mm"), fromRegion: nil).absoluteDate
            } catch {
                return Date()
            }
        }
    }
    
    var show_day:String?
    
    /// 商品信息
    var arr:[HpProductModel]!
    
    func isEqual(item:MiaoShaModel) -> Bool {
        guard self.active == item.active else {
            return false
        }
        guard self.time == item.time else {
            return false
        }
        guard self.lefttime == item.lefttime else {
            return false
        }
        guard self.arr.count == item.arr.count else {
            return false
        }
        return true
    }
    
    
    // MARK: 实现MikerSwiftJSONAble 协议， 解析json数据
    required  init?(json: JSON) {
        descriptionValue = json[kMiaoShaDescriptionValueKey].stringValue
        active = json[kMiaoShaActiveKey].intValue
        time = json[kMiaoShaTimeKey].stringValue
        lefttime = json[kMiaoShaLefttimeKey].doubleValue/1000
        arr = []
        if let items = json["arr"].array {
            for item in items {
                if let productModel = HpProductModel(json: item){
                    arr?.append(productModel)
                }
            }
        } else {
            arr = []
        }
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(descriptionValue as AnyObject, forKey: kMiaoShaDescriptionValueKey)
        
        dictionary.updateValue(active as AnyObject, forKey: kMiaoShaActiveKey)
        
        dictionary.updateValue(time as AnyObject, forKey: kMiaoShaTimeKey)
        
        dictionary.updateValue(lefttime as AnyObject, forKey: kMiaoShaLefttimeKey)
        
        
        return dictionary
    }
    
    
}
